﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace DynamicForm.Manager
{
    public class DataAccess
    {
        public static DataSet RunSelectStatement(string query)
        {
            DataSet result = new DataSet();
            String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["DynoObjectConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(strConnString);
            SqlCommand cmd = new SqlCommand(query);
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            SqlDataAdapter sda = new SqlDataAdapter();
            try
            {
                con.Open();
                sda.SelectCommand = cmd;
                sda.Fill(result);
            }
            catch
            {
                return null;
            }
            finally
            {
                con.Close();
                cmd.Dispose();
                sda.Dispose();
            }
            
            return result;
        }

        public static void ExecuteNonQuery(string query)
        {
            int result = 0;
            String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["DynoObjectConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(strConnString);
            SqlCommand cmd = new SqlCommand(query);
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            try
            {
                con.Open();
                result = cmd.ExecuteNonQuery();
            }
            catch
            {
                throw new Exception("Error during insert");
            }
            finally
            {
                con.Close();
                cmd.Dispose();
            }
        }
    }
}